//
//  SDInfoController.m
//  SuperDoctor
//
//  Created by 陶宏路 on 16/7/6.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDInfoController.h"
#import "SDTableViewCell.h"
#import "SDHeadView.h"
#import "SDCollectionCell.h"
#import "SDInfoTableController.h"
#import "SDConsultController.h"
@interface SDInfoController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong) UITableView * infoTable;
@property (nonatomic,strong) UIView * lineView;
@property (nonatomic,strong) SDHeadView * headView;
@property (nonatomic,strong) UIButton * leftButton;
@property (nonatomic,strong) UIButton * rightButton;
@property (nonatomic,strong) UIView * sLineView;
@property (nonatomic,strong) UICollectionView * collection;
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
@property (nonatomic,strong) UIView * scrollLine;
@property (nonatomic,strong) UIScrollView * scrollView;
@property (nonatomic,assign) BOOL  index;
@property (nonatomic,strong) UIButton * consultButton;
@property (nonatomic,strong) UIButton * button;
@end

static int buttonIndex = 0;

@implementation SDInfoController

// collection 懒加载
-(UICollectionView *)collection{
    
    if (_collection == nil) {
        
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        self.layout = layout;
        _collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        [self.view addSubview:_collection];
    }
    
    return _collection;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setButton];
    [self setUpView];
    [self setUpLayer];
    self.headView.model = self.model;
    [self.collection registerClass:[SDCollectionCell class] forCellWithReuseIdentifier:@"homecell"];
}

// 左上角按钮的图片
-(void)setButton{
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    self.button = button;
    [button setBackgroundImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didStarButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
}

-(void)didStarButton:(UIButton*)sender{
    
    if (buttonIndex == 0) {
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"shixin"] forState:UIControlStateNormal];
        buttonIndex++;
        
    }else{
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
        buttonIndex--;
    }
}
// -------------------实例化控件------------------

-(void)setUpView{
    
//    顶部医生简介
    SDHeadView * headView = [[SDHeadView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:headView];
    self.headView = headView;
    
//   简介下方灰色分割线
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineView];
    self.lineView = lineView;
    
//   两个button 之间的分割线
    UIView * sLineView = [[UIView alloc]init];
    sLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:sLineView];
    self.sLineView = sLineView;
    
//   左边按钮
    UIButton * leftButton = [[UIButton alloc]init];
    [leftButton setTitle:@"就诊条件" forState:UIControlStateNormal];
    [leftButton setTitleColor:kThemeColor forState:UIControlStateNormal];
    self.leftButton = leftButton;
    [self.leftButton addTarget:self action:@selector(didClickButton) forControlEvents:UIControlEventTouchUpInside];
   
//   右边按钮
    UIButton * rightButton = [[UIButton alloc]init];
    [rightButton setTitle:@"医生简介" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.rightButton = rightButton;
    [self.rightButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:leftButton];
    [self.view addSubview:rightButton];
    
//   按钮下方滚动 scrollView
    UIScrollView * scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];

    self.scrollView.userInteractionEnabled = NO;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = YES;
    
//   scrollView 内部滚动条
    UIView * scrollLine = [[UIView alloc]init];
    [self.scrollView addSubview:scrollLine];
    self.scrollLine = scrollLine;
    self.scrollLine.backgroundColor = kThemeColor;

//   底部咨询医生Button
    UIButton * consultButton = [[UIButton alloc]init];
    self.consultButton = consultButton;
    [consultButton setTitle:@"咨询医生" forState:UIControlStateNormal];
    [consultButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [consultButton setBackgroundColor:kThemeColor];
    [self.view addSubview:consultButton];
    [consultButton addTarget:self action:@selector(didClickConsultButton:) forControlEvents:UIControlEventTouchUpInside];
}


//-----------------设置控件约束---------------

-(void)setUpLayer{
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(64);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(150);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.headView.mas_bottom);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(4);
    }];
    
    [self.sLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(3);
        make.height.mas_equalTo(35);
        
    }];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
        make.right.mas_equalTo(self.sLineView.mas_left).offset(-50);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.sLineView.mas_right).offset(55);
    }];
    
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sLineView.mas_bottom).offset(3);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-60);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sLineView.mas_bottom);
        make.leading.mas_equalTo(self.view.mas_leading);
        make.trailing.mas_equalTo(self.view.mas_trailing);
        make.bottom.mas_equalTo(self.collection.mas_top);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(3);
        
    }];

    [self.scrollLine mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.mas_equalTo(self.scrollView.mas_top);
        make.bottom.mas_equalTo(self.scrollView.mas_bottom);
        make.height.mas_equalTo(3);
        make.leading.mas_equalTo(self.scrollView.mas_leading).offset(50);
        make.trailing.mas_equalTo(self.scrollView.mas_centerX).offset(-40);
        
    }];
    
    [self.consultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.collection.mas_bottom).offset(10);
        make.left.mas_equalTo(self.view.mas_left).offset(5);
        make.right.mas_equalTo(self.view.mas_right).offset(-5);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-5);
    }];
    
}


-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    self.layout.itemSize = self.collection.bounds.size;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.minimumLineSpacing = 0;
    
    self.collection.pagingEnabled = YES;
    self.collection.bounces = NO;
    self.collection.showsHorizontalScrollIndicator = YES;
    self.collection.backgroundColor = [UIColor redColor];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *1.5, 0);
    self.collection.showsVerticalScrollIndicator = NO;
    
}

// --------------tableView delegate---------------

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SDCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homecell" forIndexPath:indexPath];
    
    if (cell == nil) {
        
        cell = [[SDCollectionCell alloc]initWithFrame:CGRectZero];
    }
    
    cell.change = indexPath.row;
    
    return cell;
}

// -----------------实现联动---------------

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = self.collection.contentOffset.x / self.scrollView.frame.size.width;
    
    self.index = index;

    
}

-(void)setIndex:(BOOL)index{
    
    if (index == 1) {
        
        [self.leftButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        [self.rightButton setTitleColor:kThemeColor forState:UIControlStateNormal];

        
    }else{
        
        [self.leftButton setTitleColor:kThemeColor forState:UIControlStateNormal];
        
        [self.rightButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

        
    }
}

-(void)didClickButton:(UIButton*)sender{
    
    [self.collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
}

-(void)didClickButton{
    
    [self.collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    [self scrollViewDidEndDecelerating:scrollView];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.scrollView setContentOffset:CGPointMake(-self.collection.contentOffset.x*0.5, 0) animated:NO];
}

//----------底部按钮咨询医生------------

-(void)didClickConsultButton:(UIButton*)sender{
    
    SDConsultController * consult = [[SDConsultController alloc]init];
    
    [self jumpOtherViewController:consult];
}
// ---------测试内存释放-------
-(void)dealloc{
    
//    NSLog(@"infoController");
}

@end
